package com.malou.client.ventas.modeloprenda;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.malou.client.MalouConstants;
import com.malou.client.util.ComboboxFieldBinding;
import com.malou.client.util.Utilities;

public class ModeloPrendaDialog extends Dialog {
	private HiddenField<String> id;
	private TextField<String> nombre;
	private TextField<String> codigo;
	private NumberField precioventa;
	private ComboBox<BeanModel> tipo;
	private Button guardar;
	private Button cancelar;
	public Status status;

	private FormBinding formBindings;
	ModeloPrenda mp;
	private MalouConstants constants = GWT.create(MalouConstants.class);
	
	public ModeloPrendaDialog(){
		setLayout(new FitLayout());
		setModal(true);
		setPlain(true);
		setSize(300, 200);
		setClosable(true);
		
		FormPanel panel = new FormPanel();		
		panel.setBorders(false);
	    panel.setBodyBorder(false);
	    panel.setPadding(5);
	    panel.setHeaderVisible(false);
	    panel.setLabelWidth(110);
	    
	    formBindings = new FormBinding(panel, false);	    
	    addFields(panel);
	    
	    setButtonAlign(HorizontalAlignment.LEFT);
	    setButtons("");
	    add(panel);
	    
	    setBodyBorder(true);	    
	    setResizable(false);	    
	    setFocusWidget(nombre);
	}
	
	 @Override
	  protected void createButtons() {
	    super.createButtons();
	    
	    if (constants == null) {
	    	constants = GWT.create(MalouConstants.class);
		}
	    
	    status = new Status();
	    status.setBusy(constants.guardando());
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    cancelar = new Button(constants.cancelar());
	    
	    cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	    	  ModeloPrendaDialog.this.hide();
	      }

	    });

	    guardar = new Button(constants.guardar());
	    
	    guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {	    	  
	    	  if(validate()){
	    		status.show();
  		    getButtonBar().disable();
  		    
  			mp = new ModeloPrenda();
  			System.out.println(mp.getId());
  			if(id.getValue() != null)
  				mp.setId(Integer.valueOf(id.getValue()));
  			mp.setNombre(nombre.getValue());
  			mp.setCodigo(codigo.getValue());
  			if(Utilities.hasValue(precioventa))
    			mp.setPrecioventa(Double.valueOf(precioventa.getValue().toString()));
  			if(tipo.getValue().get("id") != null)
            	mp.setTipoprenda_id(Integer.parseInt(tipo.getValue().get("id").toString()));
  			
  			ModeloPrendas.modeloPrendaDialog = ModeloPrendaDialog.this;   			
  			ModeloPrendas.save(mp);	    			
	    	}	    		 
	    	  
	      }
	    });
	    
	    addButton(guardar);
	    addButton(cancelar);	    
	    
	  }
	 
	 protected boolean validate() {
			boolean nom = nombre.isValid();
			boolean tip = tipo.isValid();
			boolean cod = codigo.isValid();
			boolean pre = precioventa.isValid();
		    return nom && tip && cod && pre;
		  }
	 
	 public void reset(){
		  setIconStyle("icon-new");
		  setHeading(constants.modeloPrendaNuevo());
		  		  
		  id.clear();
		  nombre.clear();
		  codigo.clear();
		  codigo.setEnabled(false);
		  precioventa.clear();
		  status.hide();
		  getButtonBar().enable();
	  }
	 
	 public void setStore(ListStore<BeanModel> store){
		  //fieldBinding(campo caja de texto,campo de la clase)
		   //id, apellidos, nombres, direccion, ruc, telefono, celular, email,
		    //razonsocial, dni
		  formBindings.addFieldBinding(new FieldBinding(id, "id"));
		  formBindings.addFieldBinding(new FieldBinding(nombre, "nombre"));
		  formBindings.addFieldBinding(new FieldBinding(codigo, "codigo"));
		  formBindings.addFieldBinding(new FieldBinding(precioventa, "precioventa"));
		  formBindings.addFieldBinding(new ComboboxFieldBinding(tipo, "tipoprenda"));
		  formBindings.setStore(store);
	  }
	 
	 private void addFields(ContentPanel cp) {

			id = new HiddenField<String>();
			id.setVisible(false);
			cp.add(id);
			
			ModeloPrendas.getComboTipoPrendas();
			ModeloPrendas.loaderTipoPrenda.load();
		    
		    tipo = new ComboBox<BeanModel>();  
		    tipo.setEditable(false);
			tipo.setFieldLabel(constants.modeloPrendaTipo());
			tipo.setDisplayField("nombre");
			tipo.setEmptyText("Seleccione...");
			tipo.setWidth(150);  
			tipo.setStore(ModeloPrendas.storeTipoPrenda);  
			tipo.setTypeAhead(true);  
			tipo.setTriggerAction(TriggerAction.ALL);
			tipo.setValueField("nombre");
			tipo.setAllowBlank(false);
			tipo.setUseQueryCache(false);
		    cp.add(tipo, new FormData("-20"));
			
			codigo = new TextField<String>();
			codigo.setMinLength(3);
			codigo.setFieldLabel(constants.modeloPrendaCodigo());	    
			codigo.setAllowBlank(false);	    
		    cp.add(codigo, new FormData("-20"));
		    
		    nombre = new TextField<String>();
		    nombre.setMinLength(3);
		    nombre.setFieldLabel(constants.modeloPrendaNombre());	    
		    nombre.setAllowBlank(false);	    
		    cp.add(nombre, new FormData("-20"));
		    
		    precioventa = new NumberField();
		    
		    precioventa.setMinLength(2);
		    precioventa.setFieldLabel(constants.modeloPrendaPrecioVenta());	    
		    precioventa.setAllowBlank(false);	    
		    
		    precioventa.setFormat(NumberFormat.getFormat("0.00"));
		    precioventa.setAllowNegative(false);
		    cp.add(precioventa, new FormData("-20"));		    
 
	   }
	 
	 public void beforeEdit(){
		  setIconStyle("icon-update");
		  setHeading(constants.modeloPrendaModificar());		  
		  
		  status.hide();
		  getButtonBar().enable();
		  codigo.setEnabled(true);
	  }
	  public void bind(BeanModel selectedModel){		  
		  formBindings.bind(selectedModel);
	  }
	  public void unbind(){
		  formBindings.unbind();
	  }
}
